﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class admin_city_manager : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ctp_lib.check_login(this.Page, "admin/city_manager.aspx", false);

        if ((int)Session["userno"] != 1 && (int)Session["userno"] != 9)
        {
            Response.Write(Session["userno"].ToString());
            Response.Write("error");
            this.Page.Visible = false;
            return;
        }

        if (!this.IsPostBack)
        {
            this.Label1.Visible = false;

            ctpDataContext db = new ctpDataContext();

            var data = from p in db.ct_profiles
                       join q in db.users on p.manager_user_no equals q.no
                       where p.manager_user_no != null
                       select new
                       {
                           title=p.title,
                           no=p.no,
                           userno=q.no,
                           nickname=q.nickname

                       };

            this.GridView1.DataSource = data;
            this.DataBind();

        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        ctpDataContext db = new ctpDataContext();
        ct_profile ct = null;

        try
        {
             ct = db.ct_profiles.Single(p => p.title == this.TextBox1.Text.ToLower().Trim());
        }
        catch (Exception e1)
        {
            this.Label1.Text = "no this city, please try again";
            this.Label1.Visible = true;
            return;
        }

        try
        {
            user u = db.users.Single(p => p.no == int.Parse(this.TextBox2.Text));
        }
        catch (Exception e2)
        {
            this.Label1.Text = "no this user, please try again";
            this.Label1.Visible = true;
            return;

        }

        ct.manager_user_no = int.Parse(this.TextBox2.Text);
        db.SubmitChanges();

        Response.Redirect("city_manager.aspx");

    }


    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (this.TextBox3.Text.Trim() == "") return;

        ctpDataContext db = new ctpDataContext();
        ct_profile ct=null;

        try
        {
             ct = db.ct_profiles.Single(p => p.title == this.TextBox3.Text.Trim().ToLower());
        }
        catch (Exception e1)
        {
            return;
        }

        ct.manager_user_no = null;
        db.SubmitChanges();

        Response.Redirect("city_manager.aspx");

    }
}
